package com.stormragetech.arthas.dispatcher;

import com.stormragetech.arthas.common.exception.ArthasException;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.HttpServerExpectContinueHandler;
import io.netty.handler.ssl.SslContext;
import lombok.extern.log4j.Log4j2;

/**
 * description: arthas channel initializer, initialize netty channel
 *
 * @author: Xue Bin
 * @version: 1.0.0-SNAPSHOT
 */
@Log4j2
public class ArthasChannelInitializer extends ChannelInitializer<SocketChannel> {

    private final SslContext ssl;

    public ArthasChannelInitializer(SslContext ssl) {
        this.ssl = ssl;
    }

    @Override
    protected void initChannel(SocketChannel channel) {

        ChannelPipeline pipeline = channel.pipeline();
        if (null != ssl) {
            pipeline.addLast(ssl.newHandler(channel.alloc()));
        }
        pipeline.addLast(new HttpServerCodec());
        pipeline.addLast(new HttpObjectAggregator(1024 * 1024 * 64));
        pipeline.addLast(new HttpServerExpectContinueHandler());
        pipeline.addLast(new ArthasHttpHandler());
    }
}
